<?php

class Member_RegisterController extends Zend_Controller_Action
{
    function init()
    {
        $this->_helper->acl->allow('guest', 'menu');
        $this->_helper->acl->allow('guest', 'advert');
        $this->_helper->acl->allow(null);
    }
     
    public function preDispatch()
    {
        //INFO : sert à quoi?
    	$this->_request->setParam('format', 'json');
        $this->_helper->ajaxContext()->initContext();
    }

    public function indexAction()
    {
    	$this->_auth = Zend_Auth::getInstance();
    	if ($this->_auth->hasIdentity())
    		$this->_redirect('/');//OPTIMIZEME: à parametrer (c mieux g pense)    	
    	
    	$formUrl = $this->view->url(array('module'=>'member','controller'=>'register'));
        $form = new RegisterForm($formUrl);        
        if ($this->_request->isPost()) {
        	$formData = $this->_request->getPost();        	
            if ($form->isValid($formData)) {
            	$user = new Users();
                if($val = $user->registerUser($formData))//or $this->_request->getPost()
                	 $this->_redirect('/');
            } else {
                $this->view->formResponse = 'Sorry, there was a problem with your submission. Please check the following:';
                $form->populate($formData);
            }
        }        
        $this->view->form = $form;
    }   
}